﻿using BYUStock.Comman;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BYUStock.View
{
    public partial class StockHistoryForm : Form
    {
        DateTime Start { get; set; }
        CandleSpan Span { get; set; }
        int CandlesOfView = 30;
        float WheelScaler = 2048;
        CandleCollection CurrCandles = null;
        CandleChart Chart { get; set; } = null;

        public StockHistoryForm()
        {
            this.Start = DateTime.Now;
            this.Span = CandleSpan.Day;
            InitializeComponent();
        }

        private void StockHistoryForm_Load(object sender, EventArgs e)
        {
            InitPlot();
        }

        private void rbtn_day_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtn_day.Checked)
            {
                Span = CandleSpan.Day;
                RefreshPlot();
            }
        }

        private void rbtn_min1_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtn_min1.Checked)
            {
                Span = CandleSpan.Minute1;
                RefreshPlot();
            }
        }

        void InitPlot()
        {
            Chart = new CandleChart(null);
            Chart.TopLevel = false;
            Chart.FormBorderStyle = FormBorderStyle.None;
            Chart.Dock = DockStyle.Fill;
            this.Controls.Add(Chart);
            Chart.Show();
            Chart.ViewMouseWheel += (mouseWheel) =>
            {
                var delta = (int)(mouseWheel / WheelScaler * CurrCandles.Candles.Count) + (mouseWheel > 0 ? 1 : -1);
                if (delta > 0)
                {
                    CurrCandles.Skip(delta);

                    var startIndex = CurrCandles.Candles.Count - delta;
                    CurrCandles.Remove(startIndex);
                }
                else
                {
                    var start = Start;
                    var timeSpan = CandleParser.GetCandleTiemSpan(Span);
                    for (int i = 0; i < -delta; i++)
                    {
                        start = start.Subtract(timeSpan);
                    }
                    Start = start;
                    var candlesPre = Simulator.MakeCandles(Start, Span, -delta);
                    CurrCandles.Insert(candlesPre.Candles);
                    var end = Start;
                    for (int i = 0; i < CurrCandles.Candles.Count - delta; i++)
                    {
                        end = end.Add(timeSpan);
                    }
                    var candlesPost = Simulator.MakeCandles(end, Span, -delta);
                    CurrCandles.Add(candlesPost.Candles);
                }
                RefreshPlot(false);
            };
        }


        void RefreshPlot(bool isRefreshData = true)
        {
            if (isRefreshData)
            {
                CurrCandles = Simulator.MakeCandles(Start, Span, CandlesOfView);
            }
            Chart.SetCandles(CurrCandles);
        }
    }
}
